﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BLL;
using System.Text.RegularExpressions;
namespace PhongMachTu.GUI
{
    public partial class frmThemThuoc : Form
    {
        public frmThemThuoc()
        {
            InitializeComponent();
        }
        public event EventHandler ButtonCT_Clicked;
        public int strMaKhamBenh;
        ChiTietKhamBenhBLL bllCTKhamBenh = new ChiTietKhamBenhBLL();
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                CHITIETKHAMBENH ct = new CHITIETKHAMBENH();
                ct.MaThuoc = cbThuoc.SelectedValue.ToString();

                ct.MaDonVi = cbDonVi.SelectedValue.ToString();
                Regex regex = new Regex("^[\\d]+$");
                if (regex.IsMatch(txtSoLuong.Text))
                {
                    ct.SoLuong = Convert.ToInt32(txtSoLuong.Text);
                }
                else
                {
                    MsgBox.Info("Vui lÒNG nhẬp sỐ !");
                }
                ct.MaCachDung = cbCachDung.SelectedValue.ToString();
                ct.MaKhamBenh = strMaKhamBenh;

                if (bllCTKhamBenh.ThemChiTietKhamBenh(ct))
                {
                    MsgBox.Success("Thêm thành công !");
                    if (this.ButtonCT_Clicked != null)
                        this.ButtonCT_Clicked(sender, e);
                    this.Close();
                }
                else
                {
                    MsgBox.Error("Xảy ra lỗi !");
                }
            }
            catch (Exception ex)
            {
                MsgBox.Error(ex);

            }
        }
        ThuocBLL bllThuoc = new ThuocBLL();
        DonViBLL bllDonVi = new DonViBLL();
        CachDungBLL bllCachDung = new CachDungBLL();
        private void frmThemThuoc_Load(object sender, EventArgs e)
        {
            cbThuoc.DataSource = bllThuoc.LayDSThuoc();
            cbThuoc.DisplayMember = "TenThuoc";
            cbThuoc.ValueMember = "MaThuoc";

            cbDonVi.DataSource = bllDonVi.LayDSDonVi();
            cbDonVi.DisplayMember = "TenDonVi";
            cbDonVi.ValueMember = "MaDonVi";

            cbCachDung.DataSource = bllCachDung.LayDSCachDung();
            cbCachDung.DisplayMember = "TenCachDung";
            cbCachDung.ValueMember = "MaCachDung";
        }

        private void btnLuuVaThem_Click(object sender, EventArgs e)
        {
            try
            {
                CHITIETKHAMBENH ct = new CHITIETKHAMBENH();
                ct.MaThuoc = cbThuoc.SelectedValue.ToString();

                ct.MaDonVi = cbDonVi.SelectedValue.ToString();
                Regex regex = new Regex("^[\\d]+$");
                if (regex.IsMatch(txtSoLuong.Text))
                {
                    ct.SoLuong = Convert.ToInt32(txtSoLuong.Text);
                }
                else
                {
                    MsgBox.Info("Vui lÒNG nhẬp sỐ !");
                }
                ct.MaCachDung = cbCachDung.SelectedValue.ToString();
                ct.MaKhamBenh = strMaKhamBenh;

                if (bllCTKhamBenh.ThemChiTietKhamBenh(ct))
                {
                    MsgBox.Success("Thêm thành công !");
                    if (this.ButtonCT_Clicked != null)
                        this.ButtonCT_Clicked(sender, e);
              
                }
                else
                {
                    MsgBox.Error("Xảy ra lỗi !");
                }
            }
            catch (Exception ex)
            {
                MsgBox.Error(ex);

            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
